package com.kanshu.ksgb.fastread.doudou.base.baseui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.kanshu.ksgb.fastread.doudou.app.constants.ARouterConfig;
import com.kanshu.ksgb.fastread.doudou.common.util.Utils;
import com.kanshu.ksgb.fastread.doudou.common.business.commonbean.NotifyBean;
import com.umeng.analytics.MobclickAgent;

/**
 * 项目名称：client
 * 类描述：h5页面基类
 * 创建人：wanchunyang
 * 创建时间：2017/4/28 15:55
 * 修改人：wanchunyang
 * 修改时间：2017/4/28 15:55
 * 修改备注：
 */
@Route(path = ARouterConfig.COMMON_H5)
public class CommonH5Activity extends BaseActivity {
    public static final String EXTRA_PARAM_URL = "url";
    public static final String EXTRA_PARAM_TITLE = "title";
    public static final String EXTRA_PARAM_EXTEND = "extend";
    public static final String EXTRA_PARAM_NEEDINJECTJS = "needinjectjs";
    public static final String EXTRA_IS_SHOW_LOADING = "is_show_loading";
    public static final String EXTRA_IS_HIDE_TITLE = "hide_title";
    private String mTitleStr;
    private String mUrl;
    private boolean isShowLoading;
    //扩展参数 用于加入头部参数
    private Bundle mExtendParams;
    private static long sClickTime;
    private BaseH5Fragment mFragment;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        parseIntent();
        showH5Fragment();
        mTitle.getLeftContainer().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mFragment != null && mFragment.isAdded() && mFragment.getWebview() != null) {
                    WebView webView = mFragment.getWebview();
                    if (webView.canGoBack()) {
                        webView.goBack();   //后退
                        return;
                    } else {
                        finish();
                    }
                }
            }
        });
    }
    public void parseIntent() {
        mUrl = getIntent().getStringExtra(EXTRA_PARAM_URL);
        String hide = getIntent().getStringExtra(EXTRA_IS_HIDE_TITLE);
        mTitleStr = getIntent().getStringExtra(EXTRA_PARAM_TITLE);
        mExtendParams = getIntent().getBundleExtra(EXTRA_PARAM_EXTEND);
        isShowLoading = getIntent().getBooleanExtra(EXTRA_IS_SHOW_LOADING, false);
        if (!TextUtils.isEmpty(mTitleStr)) {
            setTitle(mTitleStr);
        }
        if (!TextUtils.isEmpty(hide) && TextUtils.isDigitsOnly(hide)) {
            if (Integer.parseInt(hide) == 1) {
                goneTitlebar();
            }
        }
        parseVipJpushChannel();
    }

    protected void parseVipJpush(NotifyBean bean){
        mUrl = bean.notify_url;
    }

    protected void showH5Fragment() {
        BaseH5Fragment fragment = provideH5Fragment();
        Bundle bundle = new Bundle();
        bundle.putString(BaseH5Fragment.EXTRA_PARAM_URL, mUrl);
        bundle.putBoolean(BaseH5Fragment.EXTRA_IS_SHOW_LOADING, isShowLoading);
        bundle.putBundle(BaseH5Fragment.EXTRA_PARAM_EXTEND, mExtendParams);
        bundle.putBoolean(BaseH5Fragment.EXTRA_PARAM_NEEDINJECTJS, true);
        fragment.setArguments(bundle);
        showFragment(fragment, this.getClass().getSimpleName());
        mFragment = fragment;
    }

    protected BaseH5Fragment provideH5Fragment() {
        BaseH5Fragment fragment = new BaseH5Fragment();
        return fragment;
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(getClass().getSimpleName());
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(getClass().getSimpleName());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Utils.jumpToHomeIfNeed();
    }

    @Override
    public void onBackPressed() {
        if (mFragment != null && mFragment.isAdded() && mFragment.getWebview() != null) {
            WebView webView = mFragment.getWebview();
            if (webView.canGoBack()) {
                webView.goBack();   //后退
                return;
            }
        }
        super.onBackPressed();
    }
    @Override
    public void finish() {
        Utils.jumpToHomeIfNeed();
        super.finish();
    }

}
